package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.service.cargo.PackingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;


@Service
public class PackingServiceImpl implements PackingService {

    //注入集装箱Dao
    @Autowired
    private PackingDao packingDao;

    //注入报运单Dao
    @Autowired
    private ExportDao exportDao;
    /**
     * 根据集装箱单删除装箱单
     *
     * @param packingListId
     */
    @Override
    public int deleteByPrimaryKey(String packingListId) {
        return packingDao.deleteByPrimaryKey(packingListId);
    }


    /**
     * 新增装箱单
     *
     * @param packing
     */
    @Override
    public int save(Packing packing) {
        //把信用证号 装运港 目的港 收货人 运输方式 添加到装箱单
        String[] exportIds = packing.getExportIds().split(",");
        Export export = exportDao.selectByPrimaryKey(exportIds[0]);

        BeanUtils.copyProperties(export,packing);

        packing.setPackingListId(UUID.randomUUID().toString());
        packing.setState(0);
        return packingDao.insertSelective(packing);
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table co_packing
     *
     * @param example
     * @mbg.generated
     */
    @Override
    public List<Packing> selectByExample(PackingExample example) {
        return packingDao.selectByExample(example);
    }

    /**
     * 根据集装箱编号查看装箱单
     *
     * @param packingListId
     */
    @Override
    public Packing selectByPrimaryKey(String packingListId) {
        return packingDao.selectByPrimaryKey(packingListId);
    }

    /**
     * 更新装箱单
     *
     * @param packing
     */
    @Override
    public int update(Packing packing) {
        return packingDao.updateByPrimaryKeySelective(packing);
    }

    /**
     * 根据id查询装箱单
     *
     * @param packingListId
     */
    @Override
    public Packing findByPLid(String packingListId) {
        return packingDao.selectByPrimaryKey(packingListId);
    }

    /**
     * 分页查询
     *
     * @param packingExample
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Packing> findByPage(PackingExample packingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(packingDao.selectByExample(packingExample));
    }
}
